// 投票
class vote extends eui.Component{
    private btnno:eui.Image;
    private btnyes:eui.Image;
    private title:eui.Group;
    private alphaBg:eui.Image;
    private uiContent:eui.Image;

    private tx_title1:eui.Label;
    private tx_title2:eui.Label;
    private btntx1:eui.Label;
    private btntx2:eui.Label;
    private tx:eui.Label;
    public RETURN_VALUE:string = "returnValue";

    public constructor(){
        super();
        this.once(eui.UIEvent.COMPLETE,this.onComplete,this);
        this.skinName = "src/Game/gameRoom/window/voteSkin.exml";;
    }
    private onComplete(e:eui.UIEvent){
        this.btnno.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onBtn1,this);
        this.btnyes.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onBtn2,this);
        this.once(egret.Event.REMOVED_FROM_STAGE,this.onRemove,this);
        StageData.MainObj.addEventListener(Events.STAGE_SIZE,this.onMainSize,this);
    }
    private onRemove(e:egret.Event){ 
        StageData.MainObj.removeEventListener(Events.STAGE_SIZE,this.onMainSize,this);
        this.btnno.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onBtn1,this);
        this.btnyes.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onBtn2,this);       
    }
    private onMainSize(){
        this.alphaBg.width = StageData.W;
        this.alphaBg.height = StageData.H;
        this.uiContent.x = (this.alphaBg.width - this.uiContent.width)/2;
        this.uiContent.y = (this.alphaBg.height - this.uiContent.height)/2;
        this.contentBg.y = this.uiContent.y + 126;
        this.contentBg.x = this.uiContent.x + (this.uiContent.width - this.contentBg.width)/2;
        if(this.timerObj){
            this.timerObj.x = this.uiContent.x + this.tx.x + this.tx.textWidth + 5;
            this.timerObj.y = this.uiContent.y + this.tx.y+2;
        }
    }
    private onBtn1(e:egret.TouchEvent){
        StageData.soundObj.soundSinglePlay("click");
        this.dispatchEventWith(this.RETURN_VALUE,false,false);
    }
    private onBtn2(e:egret.TouchEvent){
        StageData.soundObj.soundSinglePlay("click");
        this.dispatchEventWith(this.RETURN_VALUE,false,true);
    }
    public hideBtn(){
        this.btnno.visible = false;
        this.btnyes.visible = false;
        this.btntx1.visible = false;
        this.btntx2.visible = false;
    }
    private contentBg:egret.Sprite = new egret.Sprite();
    private timerObj:TimerCountDown;

    private updateVoteData(seatIndexName:string,closeTime:any,applyLeaveIndex:number){
        this.tx_title1.text = seatIndexName;
        this.tx_title1.x = 0;
        this.tx_title2.x = this.tx_title1.x + this.tx_title1.textWidth+5;
        this.title.x = (this.uiContent.width-this.title.width)/2;

        this.timerObj = new TimerCountDown("",0xB34600);
        this.timerObj.x = this.uiContent.x + this.tx.x + this.tx.textWidth + 5;
        this.timerObj.y = this.uiContent.y + this.tx.y+2;
        this.timerObj.startTimer(closeTime);
        this.addChild(this.timerObj);

        //隐藏按钮
        if(applyLeaveIndex == GameData.mySeatIndex){
            this.hideBtn();
        }
    }
    //设置内容（申请投票人座位号，结束时间，投票为同意的座位号）
    public setContent(applyLeaveIndex:number,roomCancelTimeMillis:any,voteSeatIndexList:Array<number>){
       //if(applyLeaveIndex == -1||roomCancelTimeMillis == -1) return;
       StageData.soundObj.soundSinglePlay("down");
        var userInfo:any = GameData.userData;
        
        for(var i:number=this.contentBg.numChildren;i>0;i--){
            this.contentBg.removeChildAt(i-1);
        }

         this.updateVoteData(userInfo[GameData.getUserSeatIndex(applyLeaveIndex)].nickname,roomCancelTimeMillis,applyLeaveIndex);
       
        for(var i:number=0;i<userInfo.length;i++){
            var obj:playImageVote = new playImageVote();
            obj.x = (obj.width+10)*i;
            this.contentBg.addChild(obj);
            obj.setContent(userInfo[i].headUrl,userInfo[i].nickname);            
            obj.setOffLine(userInfo[i].offline);
            //计算是否同意
            let isTongyi:boolean= false;
            for(var n=0;n<voteSeatIndexList.length;n++){
                if(userInfo[i].seatIndex == voteSeatIndexList[n]){
                    isTongyi = true;
                    break;
                }
            }        
            if(isTongyi){
                obj.setVote(true);
                if(voteSeatIndexList[n] == GameData.mySeatIndex){
                    this.hideBtn();
                }                           
            }else{
                obj.setVote(false);
            }
         }
        //this.contentBg.y = this.uiContent.y + 106;
        //this.contentBg.x = this.uiContent.x + (this.uiContent.width - this.contentBg.width)/2; 
        this.addChild(this.contentBg);
        this.onMainSize();
    }    
}